package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.greendao.EProblemEntityDao;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.greenentity.ys.EProblemEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddProblemActivity extends BaseActivity {

    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.search_info)
    EditText searchInfo;
    @BindView(R.id.list2)
    RecyclerView list2;
    DaoSession session;

    SuperAdapter<String> leftAdapter;
    int left = 0;//记录left的选择
    List<EProblemEntity> deviceEntities = new ArrayList<>();
    SuperAdapter<EProblemEntity> rightAdapter;
    String label, search, intentLabel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_quantities;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        session = GreenDaoManager.getInstance().getmDaoSession();
        intentLabel = getIntent().getStringExtra("label");
        List<String> labels = XUtils.distinctQuery(EProblemEntityDao.Properties.Smalltype.columnName
                , EProblemEntityDao.TABLENAME, EProblemEntityDao.Properties.Bigtype.columnName + " = '" + intentLabel + "'");
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
        list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        list.setAdapter(leftAdapter = new SuperAdapter<String>(R.layout.content_adddevice_left) {
            @Override
            public void bindView(ViewHolder viewHolder, final String s, final int i) {
                TextView t = viewHolder.getView(R.id.label);
                t.setText(s);
                if (i == left) {
                    t.setBackgroundResource(R.drawable.view_bottom_textpre);
                } else {
                    t.setBackgroundResource(R.color.white);
                }
                t.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchInfo.setText("");
                        label = s;
                        left = i;
                        leftAdapter.notifyDataSetChanged();
                        setRightList();
                    }
                });
            }
        });
        leftAdapter.setData(labels);
        leftAdapter.setEmptyDataView(R.layout.view_empty);
        list2.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
        list2.setAdapter(rightAdapter = new SuperAdapter<EProblemEntity>(R.layout.content_add_problem) {
            @Override
            public void bindView(ViewHolder viewHolder, final EProblemEntity b, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(b.getCommon());
                ((TextView) viewHolder.getView(R.id.description)).setText(b.getAnalysis());
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("problem", b);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                });
            }
        });
        rightAdapter.setData(deviceEntities);
        rightAdapter.setEmptyDataView(R.layout.view_empty);
        if (labels.size() > 0) {
            label = labels.get(0);
            setRightList();
        }
    }


    private void setRightList() {
        deviceEntities.clear();
        search = searchInfo.getText().toString();
        QueryBuilder<EProblemEntity> builder = session.getEProblemEntityDao().queryBuilder();
        deviceEntities.addAll(builder.where(
                EProblemEntityDao.Properties.Bigtype.eq(intentLabel)
                , EProblemEntityDao.Properties.Smalltype.eq(label)
                , builder.or(EProblemEntityDao.Properties.Common.like("%" + search + "%")
                        , EProblemEntityDao.Properties.Analysis.like("%" + search + "%"))
        )
                .list());
        rightAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.search_button)
    public void onViewClicked() {
        setRightList();
    }
}
